package com.zhang.controller.brand;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhang.brand.entity.BrandCategory;
import com.zhang.brand.service.IBrandCategoryService;
import com.zhang.common.util.result.R;
import com.zhang.common.util.result.RUtils;
import com.zhang.goods.entity.Category;
import com.zhang.goods.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 分类与品牌的关联表 前端控制器
 * </p>
 *
 * @author 创世神
 * @since 2023-09-11
 */
@RestController
@RequestMapping("/brand/brandCategory/")
public class BrandCategoryController {
    @Autowired
    private IBrandCategoryService brandCategoryService;
    @Autowired
    private ICategoryService categoryService;


    @GetMapping("get/byBrandId/{brandId}")
    R getListFromBrandId(@PathVariable Long brandId) {
        List<BrandCategory> list = brandCategoryService.list(new QueryWrapper<BrandCategory>().eq("brand_id", brandId));
        return RUtils.successData(list);
    }

    @GetMapping("get/byCategoryId/{categoryId}")
    R getBrandByCategoryId(@PathVariable Long categoryId) {
        List<BrandCategory> list = brandCategoryService.list(new QueryWrapper<BrandCategory>().eq("category_id", categoryId));
        return RUtils.successData(list);
    }

    @PostMapping("add")
    R addBrandCategory(@RequestBody BrandCategory brandCategory) {
        Category byId = categoryService.getById(brandCategory.getCategoryId());
        brandCategory.setCategoryName(byId.getName());
        boolean save = brandCategoryService.save(brandCategory);
        return save ? RUtils.successMsg("新增成功") : RUtils.err("新增失败");
    }
}
